<template>
	<view>
		<view class="address" v-for="(item,index) in list" :key="index">
			<view class="address-info"><text>{{item.user_name}}</text>{{item.phone}}</view>
			<view class="address-area"><text v-if="item.default == 1">默认</text>{{item.pre_address}}{{item.address}}</view>
			<view class="address-operate">
				<view class="address-operate-edit" @click="edit(item)">编辑</view>
				<view class="address-operate-del" @click="delArea(index,item.unid)">删除</view>
			</view>
		</view>
		<navigator hover-class="false" class="addArea" url="addArea">添加新地址</navigator>
	</view>
</template>

<script>
	export default{
		data() {
			return {
				list: [], //地址列表
			}
		},
		onShow() {
			this.getList();
		},
		methods: {
			// 获取地址列表
			getList() {
				this.$sapi.apiPost('v1/' + this.market_id + '/mobile/address/infos').then(res => {
					if (res.data.code == 200 && res.data.data.length > 0) {
						this.list = res.data.data;
					} else {
						this.list = [];
					}
				})
			},
			// 删除地址
			delArea(index,id) {
				var _this = this;
				uni.showModal({
					title: '删除提示',
					content: '确定要删除该地址吗?',
					confirmColor: '#FF9900',
					success:function(res){
						if(res.confirm) {
							_this.$sapi.apiPost('v1/' + _this.market_id + '/mobile/address/delete/'+id,{unid:id}).then(res => {
								if (res.data.code == 200) {
									_this.list.splice(index,1);
								} else {
									uni.showToast({
										title:'删除失败',
										icon: 'none'
									})
								}
							})
						}
					}
				})
			},
			// 编辑地址
			edit(item) {
				uni.navigateTo({
					url: 'addArea?data='+encodeURIComponent(JSON.stringify(item))
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #F7F7F7;
	}
	.address{
		margin: 30upx 30upx 0;
		color: #A8A8A8;
		font-size: 30upx;
		background-color: #FFFFFF;
		padding: 30upx 30upx 10upx;
		border-radius: 20upx;
		.address-info{
			display: flex;
			align-items: center;
			text {
				color: #333333;
				margin-right: 10upx;
			}
		}
		.address-area{
			text{
				color: #FB5103;
				line-height: 1;
				padding: 4upx 8upx;
				margin-right: 10upx;
				white-space: nowrap;
				display: inline-flex;
				background-color: #FFEEE7;
			}
		}
		.address-operate{
			margin-top: 20upx;
			border-top: 2upx solid #F7F7F7;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			padding-top: 10upx;
			.address-operate-edit{
				color: #FF9900;
			}
			.address-operate-del{
				color: #FF0000;
				margin-left: 20upx;
			}
		}
	}
	.addArea{
		color: #FFFFFF;
		font-size: 34upx;
		width: 232upx;
		height: 100upx;
		line-height: 100upx;
		text-align: center;
		background-color: #FF9900;
		margin: 32upx auto;
	}
</style>
